<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\UserLevel;

use App\Models\UserLevels;

use Illuminate\Http\Request;


class LevelController extends Controller
{

    /**
     * 页面
     * @return view
     */
    public function index()
    {
    
        return view('admin.userLevel.index');
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request)
    {
        try {
            
            $lists = UserLevels::getList();
            
            return $this->writeJson(0, $lists);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    
    /**
     * 更新
     * @param User $request
     * @return bool
     */
    public function update(Request $request)
    {
        try {
            $data = $request->all();                  
            if ($data['id']>0) {
                $info = UserLevels::findOrFail($data['id']);
                $info->update($data);
                return $this->writeJson(0, null, '修改等级信息成功');
            } else {
                unset($data['id']);
                UserLevels::create($data);
                return $this->writeJson(0, null, '新增等级信息成功');
            }
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    

    /**
     *  单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request)
    {
        try {
            $id = $request->get('id');
            if (empty($id)) {
                return $this->writeJson(1, null, '参数错误');
            }
            if(is_array($id)){
                for ($i=0; $i<count($request['id']); $i++) {
             		 $res =UserLevels::where('id', $request['id'][$i])->delete();   // 遍历删除           
          		}

            }else{
                $where['id'] = $id;
                UserLevels::where($where)->delete();
            }
          
            return $this->writeJson(0);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

   
}
